#include "CAppStatePauseMenu.h"
#include "CAppStateGame.h"
#include "CAppStateManager.h"

CAppStatePauseMenu CAppStatePauseMenu::Instance;

CAppStatePauseMenu::CAppStatePauseMenu() {
    SurfBlack = NULL;
    Surf_Bubble = NULL;
}

void CAppStatePauseMenu::OnActivate() {
    // Load Simple Logo
    SurfBlack = CSurface::OnLoad("content/image/Black.jpg");
    SDL_SetAlpha(SurfBlack, SDL_SRCALPHA, 20);
    BlackIntens = 0;

    Surf_Bubble = CSurface::OnLoad("content/image/cube.jpg");

    Font = CFont::OnLoad("content/fonts/visitor2.ttf", 40);

    DataButton.clear();
    DataButton.push_back(CButton::OnLoad("content/image/Button/Button1.png", 160, 150 + int(DataButton.size() * 55) - 1, 0, "Continue"   , APPSTATE_GAME ));
    DataButton.push_back(CButton::OnLoad("content/image/Button/Button1.png", 160, 150 + int(DataButton.size() * 55) - 1, 1, "Save record", APPSTATE_GAMEOVER));
    DataButton.push_back(CButton::OnLoad("content/image/Button/Button1.png", 160, 150 + int(DataButton.size() * 55) - 1, 2, "Main Menu"  , APPSTATE_MENU));
    DataButton.push_back(CButton::OnLoad("content/image/Button/Button1.png", 160, 150 + int(DataButton.size() * 55) - 1, 3, "Exit"       , APPSTATE_CREDITS));

    DataButton[0].Select = true;
    hot = 0;
}

void CAppStatePauseMenu::OnDeactivate() {
    SDL_FreeSurface(SurfBlack);
    SurfBlack = NULL;
    SDL_FreeSurface(Surf_Bubble);
    Surf_Bubble = NULL;
}

void CAppStatePauseMenu::OnLoop() {
}

void CAppStatePauseMenu::OnRender(SDL_Surface* Surf_Display) {
    if (BlackIntens < 7) {
        CSurface::OnDraw(Surf_Display, SurfBlack, 0, 0);
        BlackIntens++;
    }
    for (int i = 0; i < int(DataButton.size()); i++)
        DataButton[i].OnDraw(Surf_Display, Surf_Bubble, Font);
}

CAppStatePauseMenu* CAppStatePauseMenu::GetInstance() {
    return &Instance;
}

void CAppStatePauseMenu::SelectButton(int dir) {
    if (DataButton[hot].Select) {
        DataButton[hot].Select = false;
        switch (dir) {
            case UP:
                hot -= 1;
            break;

            case DOWN:
                hot += 1;
            break;
        }
        if (hot < 0)
            hot = DataButton.size() - 1;
        else
            hot %= DataButton.size();
        DataButton[hot].Select = true;
    } else {
        DataButton[0].Select = true;
        hot = 0;
    }
}

void CAppStatePauseMenu::OnMouseMove(int mX, int mY, int relX, int relY, bool Left,bool Right, bool Middle) {
    for (int i = 0; i < int(DataButton.size()); i++)
        DataButton[i].OnLoop(mX, mY);
}

void CAppStatePauseMenu::OnLButtonDown(int mX, int mY) {
    for (int i = 0; i < int(DataButton.size()); i++)
        DataButton[i].OnClick(mX, mY);
}

void CAppStatePauseMenu::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {
    switch (sym) {
        case SDLK_UP:
            SelectButton(UP);
        break;

        case SDLK_DOWN:
            SelectButton(DOWN);
        break;

        case SDLK_RETURN:
            CAppStateManager::SetActiveAppState(DataButton[hot].GoState);
        break;

        case SDLK_ESCAPE:
            CAppStateManager::SetActiveAppState(APPSTATE_GAME);
        break;

        default: break;
    }
}

